<?php

namespace app\logic;

class LoginPassword
{
    private $obj_model_name;

    private $str_model_name;

    private $str_id_name;

    public function __construct($str_model_name)
    {
        $this->str_model_name = $str_model_name;
        switch ($str_model_name) {
            case 'user': // 用户端
                $this->obj_model_name = \app\model\UserInfo::getInstance();
                $this->str_id_name = 'user_id';
                break;
            case 'courier': // 快递端
                $this->obj_model_name = \app\model\CourierInfo::getInstance();
                $this->str_id_name = 'courier_id';
                break;
            case 'driver': // 司机端
                $this->obj_model_name = \app\model\DriverInfo::getInstance();
                $this->str_id_name = 'driver_id';
                break;
            case 'stationpc': // 站点PC端
                $this->obj_model_name = \app\model\StationInfo::getInstance();
                $this->str_id_name = 'station_id';
                break;
            case 'stationapp': // 站点手机端
                $this->obj_model_name = \app\model\StationMemberInfo::getInstance();
                $this->str_id_name = 'station_member_id';
                break;
            default:
                return false;
        }
    }

    /**
     * 设置登录密码
     * @param $int_id
     * @param $str_login_password
     * @param $str_verify_code
     * @param string $str_pass_key
     */
    public function setLoginPwd($int_id, $str_login_password, $str_verify_code, $str_pass_key = '')
    {
        _toValidate('login_pwd', $str_login_password, 10819);

        _toValidate('verify_code', $str_verify_code, 11005);

        $str_mobile = $this->obj_model_name->toValue(['id' => $int_id], 'mobile');

        // 验证短信验证码是否正确
        $bool_result = \app\logic\sms\SMS::getInstance()->verifySMS('login_pwd', $str_mobile, $str_pass_key, $str_verify_code, $this->str_model_name);
        _toCompare($bool_result, '===', false, 11005);

        $this->obj_model_name->setLoginPassword($int_id, $this->str_id_name, $str_login_password);

        _returnData();
    }

    /**
     * 设置登录密码
     * @param $int_id
     * @param $str_login_password
     * @param $str_old_login_pwd
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function setLoginPwdWithOldPwd($int_id, $str_login_password, $str_old_login_pwd)
    {
        _toValidate('login_pwd', $str_login_password, 10819); // 密码格式不正确

        _toValidate('login_pwd', $str_old_login_pwd, 10820); // 旧密码不正确

        $bool_result = $this->checkLoginPasswordIsRight($int_id, $str_old_login_pwd);
        _toCompare($bool_result, '===', false, 10820); // 旧密码不正确

        $this->obj_model_name->setLoginPassword($int_id, $this->str_id_name, $str_login_password);

        _returnData();
    }

    /**
     * 首次设置登录密码
     * @param $int_id
     * @param $str_login_password
     */
    public function setLoginPwdFirst($int_id, $str_login_password)
    {
        _toValidate('login_pwd', $str_login_password, 10819); // 密码格式不正确

        $this->obj_model_name->setLoginPassword($int_id, $this->str_id_name, $str_login_password);

        _returnData();
    }


    /**
     * 检查支付密码是否正确
     * @param $int_id
     * @param $str_login_password
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkLoginPasswordIsRight($int_id, $str_login_password)
    {
        $bool_result = $this->obj_model_name->checkLoginPasswordIsRight($int_id, $this->str_id_name, $str_login_password);

        _toCompare($bool_result, '!=', true, 12017);

        return true;
    }

    /**
     * 检查是否设置支付密码
     * @param $int_id
     * @return int 0-未设置 1-已设置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsSetLoginPassword($int_id)
    {
        return $this->obj_model_name->checkIsSetLoginPwd($int_id, $this->str_id_name);
    }
}